package com.example.uit_management;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Activity_ChangePass extends Activity {
	/**Bien button dong y*/
	private Button bt_changepass;
	/**Bien button thoat*/
	private Button bt_exit_changepass;
	/**Bien Edittext Username*/
	private EditText EText_user_changepass;
	/**Bien Edittext current Password*/
	private EditText EText_curentpass_changepass;
	/**Bien Edittext new Password*/
	private EditText EText_newpass_changepass;
	/**Bien Edittext reply new Password*/
	private EditText EText_replynewpass_changepass;
	/**Bien JsonParser_StudentPass  */
	private JsonParser_StudentPass jsonParser_StudentPass = new JsonParser_StudentPass();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pass);  
        /*Lay ID cho tung bien*/ 
        bt_changepass = (Button)findViewById(R.id.button_changepass);// lay id cho nut dong y change pass
        bt_exit_changepass=(Button) findViewById(R.id.button_exit_changepass);// lay id cho nut thoat change pass
        EText_user_changepass =(EditText)findViewById(R.id.editText_name_changepass);// lay id cho editText ten tai khoang 
        EText_curentpass_changepass =(EditText)findViewById(R.id.editText_password_current_changepass);// lay id cho editText mat khau hien tai 
        EText_newpass_changepass =(EditText)findViewById(R.id.editText_password_new_changepass);// lay id cho editText mat khau moi
        EText_replynewpass_changepass =(EditText)findViewById(R.id.editText_password_replynew_changepass);// lay id cho editText nhap lai tai khoang moi
        
     // Set listener for dong y button 
        bt_changepass.setOnClickListener(bt_changepass_NewListener);
     // Set listener for thoat button
        bt_exit_changepass.setOnClickListener(bt_exit_changepass_NewListener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //getMenuInflater().inflate(R.menu.activity__score, menu);
        return true;
    }  
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_back:{
        	Intent intent = new Intent(getApplicationContext(),MainActivity.class);
				startActivity(intent);				
				break;
        	}
        }
        return true;
    }
    /**
    Phuong thuc lang nghe cho dong y button
    Tien hanh thay doi password
    */
    private OnClickListener bt_changepass_NewListener = new OnClickListener()
    {
        public void onClick(View v)
        { 
        	/* Lay gia tri cua tung EditText */
        	String String_user_changepass =EText_user_changepass.getText().toString();// lay ten tai khoang 
            String String_curentpass_changepass=EText_curentpass_changepass.getText().toString();// lay  mat khau hien tai 
            String String_newpass_changepass=EText_newpass_changepass.getText().toString();// lay mat khau moi
            String String_replynewpass_changepass=EText_replynewpass_changepass.getText().toString();// lay nhap lai tai khoang moi
            
            if(String_user_changepass.equals("")){
            	// Chưa điện mã số sinh viên
            	Show_Alert("Mã số sinh viên không được để trống");
            }else if(String_curentpass_changepass.equals("")||String_newpass_changepass.equals("")||String_replynewpass_changepass.equals("")){
            	Show_Alert("Mật khẩu không được để trống");
            }else if(!String_newpass_changepass.equals(String_replynewpass_changepass)){
            	// nhap lai mat khau khong dung
            	// to do
            	Show_Alert("Mật khẩu nhập lại không trùng khớp với mật khẩu mới");
            }else{
            	if(jsonParser_StudentPass.Change_StudentPass(String_user_changepass, String_curentpass_changepass, String_newpass_changepass).equals("true")){
            		// Thay doi thanh cong
            		Show_Alert_backActivityMain("Thay đổi mật khẩu thành công");            			
            	}else{
            		//Thay doi khong thanh cong
            		Show_Alert("Mật khẩu không đúng");
            	}
            	
            }
        }
    };
    /**
    Phuong thuc lang nghe cho dong y button
    Tien hanh thay doi password
    */
    private OnClickListener bt_exit_changepass_NewListener = new OnClickListener()
    {
        public void onClick(View v)
        { 
        	// Trở về màng hình đăng nhập
        	finish();  
    		Intent intent = new Intent(getApplicationContext(),MainActivity.class);
			startActivity(intent);	
        }
    };
    /**
     *Ham tao dialog thong bao voi noi dung Message
     * */
    public void Show_Alert(String message){
   	 AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(Activity_ChangePass.this);
   	 myAlertDialog.setTitle("THÔNG BÁO ");   
   	 myAlertDialog.setMessage(message);

   	 myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

   	  public void onClick(DialogInterface arg0, int arg1) {
   	  // do something when the OK button is clicked
   	 
   	  }});   	
   	 myAlertDialog.show();
   }
    /**
     *Ham tao dialog thong bao voi noi dung Message sau khi nhan button OK se tro ve mang hinh chinh
     * */
    public void Show_Alert_backActivityMain(String message){
   	 AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(Activity_ChangePass.this);
   	 myAlertDialog.setTitle("THÔNG BÁO ");   
   	 myAlertDialog.setMessage(message);

   	 myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

   	  public void onClick(DialogInterface arg0, int arg1) {
   	  // do something when the OK button is clicked
   	  // Trở về màng hình đăng nhập
    	Intent intent = new Intent(getApplicationContext(),MainActivity.class);
  		startActivity(intent);
   	  }});   	
   	 myAlertDialog.show();
   }
}
